<template>
	<view class="content">
		<view class="input-group">
			<view class="input-row">
				<text class="title">server：</text>
				<m-input type="text" v-model="server" placeholder="请输入server"></m-input>
			</view>
			<view class="input-row">
				<text class="title">token：</text>
				<m-input type="password" displayable v-model="token" placeholder="请输入token" ></m-input>
			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="bindLogin">登录</button>
		</view>
	</view>
</template>

<script>
	import service from '../../service.js';
	import {
		RSAencrypt
	} from '../../common/jsencrypt.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import mInput from '../../components/m-input.vue'
	import uuidv1 from 'uuid/v1'
	export default {
		components: {
			mInput
		},
		data() {
			return {
				server: '127.0.0.1:7070',
				token: '1',
				positionTop: 0,
				isDevtools: false,
				websock: null,
			}
		},
		computed: mapState(['forcedLogin']),
		methods: {
			initPosition() {
				/**
				 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
				 * 反向使用 top 进行定位，可以避免此问题。
				 */
				this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
			},
			bindLogin() {
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 * uni.showToast({
						icon: 'none',
						title: '用户账号或密码不正确',
					});
				 */
				this.initWebSocket();

			},
			initWebSocket: function() { //初始化weosocket
				const wsuri = "ws://" + this.server;
				this.websock = new WebSocket(wsuri);
				// 监听socket连接
				this.websock.onopen = this.open
				// 监听socket错误信息
				this.websock.onerror = this.error
				// 监听socket消息
				this.websock.onmessage = this.getMessage

			},
			open: function() {
				console.log("socket连接成功")
				//连接成功事件
				let tmp = {
					"token": this.token,
					"uuid": uuidv1(),
					"type": "mobile",
					"method": "clientUp"
				};
				console.log("发送连接事件：" + RSAencrypt(JSON.stringify(tmp)));
				this.websock.send(RSAencrypt(JSON.stringify(tmp)));
			},
			error: function() {
				console.log("连接错误")
			},
			getMessage: function(msg) {
				console.log(msg.data)
			},
			send: function() {
				this.websock.send(params)
			},
			close: function() {
				console.log("socket已经关闭")
			},
			toMain(userName) {
				/**
				 * 强制登录时使用reLaunch方式跳转过来
				 * 返回首页也使用reLaunch方式
				 */
				if (this.forcedLogin) {
					uni.reLaunch({
						url: '../main/main',
					});
				} else {
					uni.navigateBack();
				}

			}
		},
		onReady() {
			this.initPosition();
			// #ifdef MP-WEIXIN
			this.isDevtools = uni.getSystemInfoSync().platform === 'devtools';
			// #endif
		},
		destroyed() {
			var that = this;
			that.websock.close() //离开路由之后断开websocket连接
		},
	}
</script>

<style>
	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
	}

	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		position: relative;
		width: 50px;
		height: 50px;
		border: 1px solid #dddddd;
		border-radius: 50px;
		margin: 0 20px;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}

	.oauth-image button {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
</style>
